import 'package:flutter/material.dart';
import 'package:ljy_p_app/common/repositories/repository_ql.dart';
import 'package:ljy_p_app/diffwidget/lj_icon_text.dart';
import 'package:ljy_p_app/diffwidget/lj_user_icon_widget.dart';
import 'package:ljy_p_app/diffwidget/ljy_card_item.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:ljy_p_app/utils/common_utils.dart';

/// 仓库Item
class ReposItem extends StatelessWidget {
  final ReposViewModel reposViewModel;

  final VoidCallback? onPressed;

  const ReposItem(this.reposViewModel, {super.key, this.onPressed});

  ///仓库item的底部状态，比如star数量等
  _getBottomItem(BuildContext context, IconData icon, String? text,
      {int flex = 3}) {
    return Expanded(
      flex: flex,
      child: Center(
        child: LjIconText(
          icon,
          text,
          LJConstant.smallSubText,
          LJColors.subTextColor,
          15.0,
          padding: 5.0,
          textWidth: flex == 4
              ? (MediaQuery.sizeOf(context).width - 100) / 3
              : (MediaQuery.sizeOf(context).width - 100) / 5,
        ),
      ),
    );  
  }

  @override
  Widget build(BuildContext context) {
    // return LjyCardItem(child: Padding(padding: EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10), child: Column(
    //   crossAxisAlignment: CrossAxisAlignment.start,
    //   children: [
    //     Row(
    //       children: [
    //         CircleAvatar(
    //           backgroundImage: NetworkImage(reposViewModel.ownerPic ?? ""),
    //         ),
    //         SizedBox(width: 5),
    //         Column(
    //           mainAxisAlignment: MainAxisAlignment.start,
    //           crossAxisAlignment: CrossAxisAlignment.start,
    //           children: [
    //             Text(reposViewModel.ownerName ?? "", style: TextStyle(fontSize: 17, fontWeight: FontWeight.w700, color: LJColors.primaryValue),),
    //             LjIconText(LJICons.REPOS_ITEM_USER, reposViewModel.repositoryName, LJConstant.smallSubLightText, LJColors.subLightTextColor, 10.0, padding: 3.0,)
    //           ],
    //         ),
    //         Expanded(child: Container()),
    //         Text(reposViewModel.repositoryType ?? "", style: LJConstant.smallSubLightText)
    //       ],
    //     ),
    //     Text(reposViewModel.repositoryDes ?? "", style: LJConstant.smallSubLightText, maxLines: 3, overflow: TextOverflow.ellipsis,),
    //     SizedBox(height: 10),
    //     Row(
    //       children: [
    //         LjIconText(LJICons.REPOS_ITEM_STAR, reposViewModel.repositoryStar, LJConstant.middleSubText, LJColors.subLightTextColor, 16.0, padding: 3.0,),
    //         SizedBox(width: 40),
    //         LjIconText(LJICons.REPOS_ITEM_FORK, reposViewModel.repositoryFork, LJConstant.middleSubText, LJColors.subLightTextColor, 16.0, padding: 3.0,),
    //         SizedBox(width: 40),
    //         (reposViewModel.hideWatchIcon == "true") ? Container() : LjIconText(LJICons.REPOS_ITEM_WATCH, reposViewModel.repositoryWatch, LJConstant.middleSubText, LJColors.subLightTextColor, 16.0, padding: 3.0,),
    //       ],
    //     ),
    //   ],
    // )));
    return LjyCardItem(
        margin: EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0, bottom: 10.0),
        child: TextButton(
            onPressed: onPressed,
            child: Padding(
              padding: EdgeInsets.only(
                  left: 0, top: 10.0, right: 10.0, bottom: 10.0),
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      LjUserIconWidget(
                        padding: EdgeInsets.only(right: 5.0, ),
                        width: 40,
                        height: 40,
                        image: reposViewModel.ownerPic,
                      ),

                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            /// 仓库名
                            Text(reposViewModel.ownerName ?? "", style: LJConstant.normalTextBold),
                            SizedBox(height: 2.0,),
                            LjIconText(LJICons.REPOS_ITEM_USER, reposViewModel.repositoryName, LJConstant.smallSubLightText, LJColors.subLightTextColor, 10, padding: 3.0,)
                          ],
                        ),
                      ),

                      /// 编程语言
                      Text(reposViewModel.repositoryType ?? "", style: LJConstant.smallSubText),
                    ],
                  ),

                  Container(
                    margin: EdgeInsets.only(top: 6.0, bottom: 2.0),
                    alignment: Alignment.topLeft,
                    child: Text(reposViewModel.repositoryDes ?? "", style: LJConstant.smallSubText, maxLines: 3, overflow: TextOverflow.ellipsis,),
                  ),

                  SizedBox(height: 20,),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      _getBottomItem(context, LJICons.REPOS_ITEM_STAR, reposViewModel.repositoryStar),
                      SizedBox(width: 5.0,),
                      _getBottomItem(context, LJICons.REPOS_ITEM_FORK, reposViewModel.repositoryFork),
                      SizedBox(width: 5.0,),
                      _getBottomItem(context, LJICons.REPOS_ITEM_ISSUE, reposViewModel.repositoryWatch, flex: 4)
                    ],
                  ),
                ],
              ),
            )));
  }
}

class ReposViewModel {
  String? ownerName;
  String? ownerPic;
  String? repositoryName;
  String? repositoryStar;
  String? repositoryFork;
  String? repositoryWatch;
  String? hideWatchIcon;
  String? repositoryType = "";
  String? repositoryDes;

  ReposViewModel();

  // ReposViewModel.fromMap(Repository data) {
  //   ownerName = data.owner!.login;
  //   ownerPic = data.owner!.avatar_url;
  //   repositoryName = data.name;
  //   repositoryStar = data.watchersCount.toString();
  //   repositoryFork = data.forksCount.toString();
  //   repositoryWatch = data.openIssuesCount.toString();
  //   repositoryType = data.language ?? '---';
  //   repositoryDes = data.description ?? '---';
  // }

  ReposViewModel.fromQL(RepositoryQL data) {
    ownerName = data.ownerName;
    ownerPic = data.ownerAvatarUrl;
    repositoryName = data.reposName;
    repositoryStar = data.starCount.toString();
    repositoryFork = data.forkCount.toString();
    repositoryWatch = data.watcherCount.toString();
    repositoryType = data.language ?? '---';
    repositoryDes =
        CommonUtils.removeTextTag(data.shortDescriptionHTML) ?? '---';
  }

  ReposViewModel.fromTrendMap(model) {
    ownerName = model.name;
    if (model.contributors.length > 0) {
      ownerPic = model.contributors[0];
    } else {
      ownerPic = "";
    }
    repositoryName = model.reposName;
    repositoryStar = model.starCount;
    repositoryFork = model.forkCount;
    repositoryWatch = model.meta;
    repositoryType = model.language;
    repositoryDes = CommonUtils.removeTextTag(model.description);
  }
}
